﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Common.Ioc;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.Proxy;
using LinFu.Proxy.Interfaces;
using LinFu.IoC;
namespace CoreEx.Common.Implementation.Proxy
{
    [PostProcessor]
    public class ProxyBuilderPostProcessor : PostProcessorBase<IProxyBuilder>
    {
        
        /// <summary>
        /// Returns a <see cref="IProxyBuilder"/> proxy instead of the original <see cref="IProxyBuilder"/> implementation.
        /// </summary>
        /// <param name="result">An <see cref="IServiceRequestResult"/> instance that represents the result of the process operation</param>        
        public override void Process(IServiceRequestResult result)
        {
            var proxyFactory = new ProxyFactory();

            var proxyBuilder = (ProxyBuilder) result.OriginalResult;

            var proxyBuilderInterceptor = result.Container.GetService<IInvokeWrapper>("ProxyBuilderInterceptor",proxyBuilder);

            var proxyBuilderProxy = proxyFactory.CreateProxy<IProxyBuilder>(proxyBuilderInterceptor);

            result.ActualResult = proxyBuilderProxy;
        }
    }
}

